//
//  BusinessTranscation.m
//  OnlineClinic
//
//  Created by lv jian on 11-11-19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RequestEngine.h"
#import "Constants.h"
#import "NAError.h"

#define NA_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }

@interface RequestEngine()

/*
 *  Execute transcation
 */
- (void) excuteTranscation:(NSString *) transcation;

@end


@implementation RequestEngine

@synthesize delegate;

#pragma mark - 
#pragma mark - Initialization and memory management

- (id) initWithTarget:(id) target action:(SEL)action {
    
    if (self = [super init]) {
        
        self.delegate = target;
        callbackAction = action;
        
        adapter = [[SBJsonStreamParserAdapter alloc] init];
        adapter.delegate = self;

        parser = [[SBJsonStreamParser alloc] init];
        parser.delegate = adapter;
        
        parser.supportMultipleDocuments = NO;
    }
    
    return self;
}

#pragma mark - 
#pragma mark - Private methods

- (void) excuteTranscation:(NSString *)transcation {
    
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:transcation]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:20.0];
    
    [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}


#pragma mark -
#pragma mark - SBJsonStreamParserAdapterDelegate methods

- (void)parser:(SBJsonStreamParser *)parser foundArray:(NSArray *)array {
    if (nil != self.delegate) {
        if ([self.delegate respondsToSelector:callbackAction]) {
            [self.delegate performSelector:callbackAction 
                                withObject:self 
                                withObject:[NAError errorWithCode:NAError_UNEXPECTED_DATA]];
        }
    }
}

- (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict {
    if (nil != self.delegate) {
        if ([self.delegate respondsToSelector:callbackAction]) {
            [self.delegate performSelector:callbackAction 
                                withObject:self 
                                withObject:dict];
        }
    }

}

#pragma mark - 
#pragma mark - NSURLConnectionDelegate methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
#ifdef DEBUG
    NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
#endif
    
	SBJsonStreamParserStatus status = [parser parse:data];
	
	if (status == SBJsonStreamParserError) {
        
        if (nil != self.delegate) {
            if ([self.delegate respondsToSelector:callbackAction]) {
                [self.delegate performSelector:callbackAction 
                                    withObject:self 
                                    withObject:[NAError errorWithCode:NAError_PARSE_FAIL]];
            }
        }
	} else if (status == SBJsonStreamParserWaitingForData) {
		NSLog(@"Parser waiting for more data");
	}
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if (nil != self.delegate) {
        if ([self.delegate respondsToSelector:callbackAction]) {
            [self.delegate performSelector:callbackAction 
                                withObject:self 
                                withObject:[NAError errorWithCode:NAError_CONNECTION_FAIL]];
        }
    }
    
    [connection release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    //[adapter release];
    [parser release];
    parser = [[SBJsonStreamParser alloc] init];
    parser.delegate = adapter;
}


#pragma mark -
#pragma mark - Public methods

- (void) shakeHandWithServer {
    
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0001"]];
}

- (void) homePageInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0002"]];
}

- (void) navigatorInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0003"]];
}

- (void) systeminlineInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0102"]];
}

- (void) detailInfos:(NSString *)ID{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@",kServerURL,@"?cmd=0202&id=",ID]];
}

- (void) shopCheckMsgWithID:(NSString *)ID q1:(NSString *)q1 q2:(NSString *)q2{

    NSLog(@"====%@",[NSString stringWithFormat:@"%@%@&id=%@&q1=%@&q2=%@",kServerURL,@"?cmd=0609",ID,q1,q2]);
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@&id=%@&q1=%@&q2=%@",kServerURL,@"?cmd=0609",ID,q1,q2]];

}

- (void) loginInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0702"]];
}

- (void) registerInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0704"]];
}

- (void) findPasswordInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0706"]];
}

- (void) retrievePasswordWithMail:(NSString *) mailAddress {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0707&q1=%@",kServerURL,mailAddress]];
}

- (void) systemInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0706"]];
}

- (void) memHomeInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0701"]];
}

- (void) contactusInfos{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0801"]];
}

- (void) contactusWithName:(NSString *)a1 
                        a2:(NSString*)a2 
                        a3:(NSString*)a3 
                        a4:(NSString*)a4 
                        a5:(NSString*)a5 
                        a6:(NSString*)a6 
                        a7:(NSString*)a7 {

    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@&a2=%@&a3=%@&a4=%@&a5=%@&a6=%@&a7=%@",kServerURL,@"?cmd=0804&a1=",a1,a2,a3,a4,a5,a6,a7]];
}

- (void) registInfos{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0901"]];
}

- (void) registWithName:(NSString *)a1 
                        a2:(NSString*)a2 
                        a3:(NSString*)a3 
                        a4:(NSString*)a4 
                        a5:(NSString*)a5 
                        a6:(NSString*)a6 
                        a7:(NSString*)a7 {
    
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@&a2=%@&a3=%@&a4=%@&a5=%@&a6=%@&a7=%@",kServerURL,@"?cmd=0904&a1=",a1,a2,a3,a4,a5,a6,a7]];
}

-(void) mapInfos{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=1001"]];

}

- (void) lastedMessages {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0002"]];
}

- (void) lastedMessagesListByPage:(NSInteger) pageNum {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0002&page=%d",kServerURL,pageNum]];
    
}

- (void) registerMemberWithUserName:(NSString *) username password:(NSString *) password {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0705&q1=%@&q2=%@&q3=%@",kServerURL,username,password,password]];
}

- (void) productInfosWithPage:(int)nPage{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%d",kServerURL,@"?cmd=0201&p=",nPage]];
}


- (void) shoppingCartInfos:(NSString *)q1{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@",kServerURL,@"?cmd=0607&q1=",q1]];
}

- (void) queryCartInfos:(NSString *)q1{
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@",kServerURL,@"?cmd=0507&q1=",q1]];
}

- (void) loginWithUserName:(NSString *) username password:(NSString *) password {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0703&q1=%@&q2=%@",kServerURL,username,password]];
}

- (void) resetPasswordPageInfo {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0708"]];
}
    
- (void) shopChecksInfos:(NSString *)q1 
                      a1:(NSString *)a1 
                      a2:(NSString *)a2 
                      a3:(NSString *)a3 
                      a4:(NSString *)a4 
                      a5:(NSString *)a5 
                      a0:(NSString *)a0{

    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@&a1=%@&a2=%@&a3=%@&a4=%@&a5=%@&a0=%@",kServerURL,@"?cmd=0608&q1=",q1,a1,a2,a3,a4,a5,a0]];

}

- (void) queryEndInfos:(NSString *)q1 
                      a1:(NSString *)a1 
                      a2:(NSString *)a2 
                      a3:(NSString *)a3 
                      a4:(NSString *)a4 
                      a5:(NSString *)a5 
                      a0:(NSString *)a0{
    
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@%@&a1=%@&a2=%@&a3=%@&a4=%@&a5=%@&a0=%@",kServerURL,@"?cmd=0508&q1=",q1,a1,a2,a3,a4,a5,a0]];
    
}

- (void) symptomWithBodyPart:(TagOfBody) bodyTag {
    
    NSString *transReq = [NSString stringWithFormat:@"%@?cmd=3&id=%d",kServerURL,bodyTag];
    [self excuteTranscation:transReq];
}

- (void) resetAccountPassword:(NSString *) accID 
                  oldPassword:(NSString *) oldPassword 
                  newPassword:(NSString *) newPassword {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0709&id=%@&q1=%@&q2=%@&q3=%@",kServerURL,accID,oldPassword,newPassword,newPassword]];
}

- (void) digitAlbumPageInfo {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0300"]];
}

- (void) digitAlbumDetailsInfoWithID:(NSString *) dID page:(NSInteger) page {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0301&c1=%@&page=%d",kServerURL,dID,page]];
}

- (void)myOrdersWithID:(NSString *)ID {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0711&id=%@", kServerURL, ID]];
}

- (void)myOrderViewWithID:(NSString *)ID Q1:(NSString *)Q1 {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0712&id=%@&q1=%@", kServerURL, ID, Q1]];
}

- (void)myQueryWithID:(NSString *)ID {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0721&id=%@", kServerURL, ID]];
}

- (void)myQueryDetailWithID:(NSString *)ID Q1:(NSString *)Q1 {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0722&id=%@&q1=%@", kServerURL, ID, Q1]];
}

- (void) digitNoteInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0400"]];
}

- (void) digitNoteDetailsWithCategoryID:(NSString *) cid keyword:(NSString *) keyword 
                                   page:(NSInteger) page {
    
    NSMutableString *reqString = [[NSMutableString alloc] init];
    [reqString appendString:kServerURL];
    [reqString appendString:@"?cmd=0401"];
    if (cid != nil) 
        [reqString appendFormat:@"&c2=%@",cid];
    
    if (keyword != nil){
        NSString *utfKeyword = [NSString stringWithUTF8String:[keyword UTF8String]];
        NSString *ctd = [utfKeyword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [reqString appendFormat:@"&q1=%@",ctd];
    }
    
    [reqString appendFormat:@"&p=%d",page];
    
    [self excuteTranscation:reqString];
    [reqString release];
}

-(void) digitAlbumLargeImageWithID:(NSString*)c1{
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0302&c1=%@", kServerURL, c1]];

}

- (void) productInfosWithID:(NSString *) pid {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0402&id=%@",kServerURL,pid]];
}

- (void) inquiryPriceInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0500"]];
}

- (void) inquiryPriceDetailsWithCategoryID:(NSString *) cid keyword:(NSString *) keyword page:(NSInteger) page {
    
    NSMutableString *reqString = [[NSMutableString alloc] init];
    [reqString appendString:kServerURL];
    [reqString appendString:@"?cmd=0501"];
    if (cid != nil) 
        [reqString appendFormat:@"&c2=%@",cid];
    
    if (keyword != nil){
        NSString *utfKeyword = [NSString stringWithUTF8String:[keyword UTF8String]];
        NSString *ctd = [utfKeyword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [reqString appendFormat:@"&q1=%@",ctd];
    }
    
    [reqString appendFormat:@"&p=%d",page];
    
    [self excuteTranscation:reqString];
    [reqString release];
}

- (void) inquiryPriceInfosWithID:(NSString *) pid {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0502&id=%@",kServerURL,pid]];
}

- (void) onlineShoppingInfos {
    [self excuteTranscation:[NSString stringWithFormat:@"%@%@",kServerURL,@"?cmd=0600"]];
}

- (void) onlineShoppingDetailsWithCategoryID:(NSString *) cid keyword:(NSString *) keyword page:(NSInteger) page {
    
    NSMutableString *reqString = [[NSMutableString alloc] init];
    [reqString appendString:kServerURL];
    [reqString appendString:@"?cmd=0601"];
    if (cid != nil) 
        [reqString appendFormat:@"&c2=%@",cid];
    
    if (keyword != nil){
        NSString *utfKeyword = [NSString stringWithUTF8String:[keyword UTF8String]];
        NSString *ctd = [utfKeyword stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [reqString appendFormat:@"&q1=%@",ctd];
    }
    
    [reqString appendFormat:@"&p=%d",page];
    
    [self excuteTranscation:reqString];
    [reqString release];

}

- (void) onlineShoppingInfosWithID:(NSString *) pid {
    [self excuteTranscation:[NSString stringWithFormat:@"%@?cmd=0602&id=%@",kServerURL,pid]];
}


@end
